package com.shop.cereshop.app.controller.imreport;

import com.shop.cereshop.app.annotation.NoRepeatSubmit;
import com.shop.cereshop.app.annotation.NoRepeatWebLog;
import com.shop.cereshop.app.service.imreport.ImReportService;
import com.shop.cereshop.commons.constant.CoReturnFormat;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.common.Page;
import com.shop.cereshop.commons.domain.imReport.CereImReport;
import com.shop.cereshop.commons.domain.imReport.dto.CereImReportParam;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/imreport")
@Slf4j(topic = "ImReportController")
@Api(value = "IM举报管理", tags = "IM举报管理")
public class ImReportController {

    @Autowired
    private ImReportService imReportService;

    /**
     * 分页查询IM举报记录
     */
    @PostMapping("page")
    @ApiOperation(value = "分页查询IM举报记录")
    public Result getReportPage(@RequestBody CereImReportParam param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }
        param.setReportUserId(user.getBuyerUserId());
        try {
            Page<CereImReport> page = imReportService.getReportPage(param);
            return Result.success(page);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("查询IM举报记录失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

    /**
     * 根据ID查询举报详情
     */
    @GetMapping("detail")
    @ApiOperation(value = "获取举报详情")
    public Result getReportDetail(@ApiParam(value = "举报ID", required = true) @RequestParam("reportId") Long reportId,
                                  HttpServletRequest request) {

        try {
            CereImReport detail = imReportService.getReportById(reportId);
            return Result.success(detail);
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取举报详情失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }

/**
 * 修改
 */
@PostMapping("update")
@ApiOperation(value = "修改举报")
public Result<Void> updateReport(@Validated @RequestBody CereImReportParam param, HttpServletRequest request) {

    try {
        Result<Void> result = imReportService.updateReport(param);
        return result;
    } catch (CoBusinessException e) {
        return Result.failed(e.getCode(), e.getMessage());
    } catch (Exception e) {
        log.error("审核举报失败", e);
        return Result.failed(CoReturnFormat.FAIL);
    }
}

    /**
     * 添加举报
     */
    @PostMapping("add")
    @NoRepeatSubmit
    @NoRepeatWebLog
    @ApiOperation(value = "添加举报")
    public Result<Void> addReport(@Validated @RequestBody CereImReportParam param, HttpServletRequest request) {
        CereBuyerUser user = (CereBuyerUser) request.getAttribute("user");
        if (user == null) {
            return new Result<>(CoReturnFormat.USER_NOT_LOGIN);
        }

        try {
            Result<Void> result = imReportService.addReport(param,user);
            return result;
        } catch (CoBusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("审核举报失败", e);
            return Result.failed(CoReturnFormat.FAIL);
        }
    }
}
